<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div class="header_log">
        <img src="../assets/heima.png" alt />
        <span class="span">欢迎来到王者荣耀</span>
      </div>
      <el-button type="info" @click="gologin">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 导航栏 -->
      <el-aside :width="isCollapse ? '56px' : '200px'">
        <div class="toggle-collapse" @click="Collapse">|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active ="activeG"
        >
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span class="list">{{item.authName}}</span>
            </template>
            <el-menu-item-group>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/'+subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="saveNavStatus('/'+subItem.path)"
              >
                <template slot="title">
                  <i class="iconfont icon-tijikongjian"></i>
                  <span class="list2">{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- main区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconsObj: {
        "125": "iconfont icon-users",
        "103": "iconfont icon-showpassword",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      isCollapse: false,
      activeG:''
    };
  },
  created() {
    this.getMenuList();
    this.activeG = window.sessionStorage.getItem('activeG')
  },
  methods: {
    gologin() {
      // 清空token
      window.sessionStorage.clear();
      // 返回到登录页面
      this.$router.push("/login");
    },
    // 获取表单数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      //   this.$message.success(res.meta.msg);
      this.menulist = res.data;
      // console.log(res.data);
    },
    // 是否折叠侧导航栏
    Collapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存链接的激活状态
    saveNavStatus(activeG){
      window.sessionStorage.setItem('activeG',activeG)
      this.activeG = activeG
    }
  }
};
</script>

<style lang='less' scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  .header_log {
    display: flex;
    align-items: center;
    .span {
      color: #fff;
      font-size: 20px;
      margin-left: 20px;
    }
  }
}
.el-container {
  height: 100%;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.list,
.list2 {
  margin-left: 10px;
}
.toggle-collapse {
  background-color: #333744;
  color: #fff;
  line-height: 24px;
  text-align: center;
  font-size: 12px;
  letter-spacing: 1.2px;
}
</style>